// 前往登录
function goLogin() {
  window.location.href = "./login.html";
}

// 前往注册
function goRegister() {
  window.location.href = "./register.html";
}

let LoginName = false;
let passWord = false;
let userName = false;
let loginNameValue;
let passWordValue;
let userNameVale;

// 校验正确提示
function AlertTitleYes(index, color, opacity, text) {
  const Title = document.getElementsByClassName("prompt")[index];
  Title.style.color = color;
  Title.style.opacity = opacity;
  Title.innerHTML = text;
}

// 登录名校验
function registerLoginName() {
  const reg = /^[\S]{1,5}$/;
  const registerLoginName = document.getElementById("registerLoginName");
  if (reg.test(registerLoginName.value)) {
    LoginName = true;
    loginNameValue = registerLoginName.value;
    AlertTitleYes(0, "#00FF00", "1", "您输入的格式正确");
  } else {
    LoginName = false;
    AlertTitleYes(0, "red", "1", "请输入1~5个任意字符");
  }
}

// 用户名校验
function registerUserName() {
  const reg = /^[\S]{1,5}$/;
  const registerUserName = document.getElementById("registerUserName");
  if (reg.test(registerUserName.value)) {
    userName = true;
    userNameVale = registerUserName.value;
    AlertTitleYes(1, "#00FF00", "1", "您输入的格式正确");
  } else {
    userName = false;
    AlertTitleYes(1, "red", "1", "请输入1~5个任意字符");
  }
}

// 密码注册校验
function registerPassWord() {
  const reg = /^[\S]{5,12}$/;
  const registerPassWord = document.getElementById("registerPassWord");
  if (reg.test(registerPassWord.value)) {
    passWord = true;
    passWordValue = registerPassWord.value;
    AlertTitleYes(2, "#00FF00", "1", "您输入的格式正确");
  } else {
    passWord = false;
    AlertTitleYes(2, "red", "1", "请输入5~12个任意字符");
  }
}

//   注册
function register() {
  const checkbox = document.getElementsByClassName("checkbox")[0];
  if (LoginName && userName && passWord && checkbox.checked) {
    axios
      .post("http://127.0.0.1:3000/api/v1/user/register", {
        loginname: loginNameValue,
        username: userNameVale,
        password: passWordValue,
      })
      .then(function (res) {
        if (res.status == 200) {
          if (res.data.code === 0) {
            titleAlert(res.data.message);
            window.location.href = "./login.html";
          } else {
            titleAlert(res.data.message);
          }
          console.log(res);
        } else {
          titleAlert("注册失败");
        }
      })
      .catch(function (error) {
        console.log(error);
        titleAlert("注册失败");
      });
  } else {
    titleAlert("请注意注册格式且同意接受协议！");
  }
}

// 登录
function Login() {
  const loginLoginName = document.getElementById("loginLoginName");
  const loginPassWord = document.getElementById("loginPassWord");
  const checkbox = document.getElementsByClassName("checkbox")[0];
  loginNameValue = loginLoginName.value;
  passWordValue = loginPassWord.value;
  if (checkbox.checked) {
    axios
      .post("http://127.0.0.1:3000/api/v1/user/login", {
        loginname: loginNameValue,
        password: passWordValue,
      })
      .then(function (res) {
        if (res.status == 200) {
          if (res.data.code === 0) {
            titleAlert("登录成功");
            localStorage.setItem('token', res.data.token);
          } else {
            titleAlert("账号或密码错误");
          }
        } else {
          titleAlert("登录失败");
        }
        console.log(res);
      })
      .catch(function (error) {
        console.log(error);
        titleAlert("登录失败");
      });
  }else {
    titleAlert('请勾选同意登录协议进行登录');
  }
}

// 提示框
function titleAlert(text) {
  const disable = document.getElementsByClassName("disable")[0];
  const Title = document.getElementsByClassName("Title")[0];
  disable.style.opacity = "1";
  disable.style.display = "block";
  Title.style.opacity = "1";
  Title.style.display = "block";
  Title.innerHTML = text;
  setTimeout(function () {
    disable.style.opacity = "0";
    Title.style.opacity = "0";
  }, 1000);

  setTimeout(function () {
    disable.style.display = "none";
    Title.style.display = "none";
  }, 2000);
}
